import vtk

def cylinder_show():
    source = vtk.vtkCylinderSource()  # 设置数据类型
    source.SetHeight(3.0)
    source.SetRadius(1.0)    # 设置半径
    source.SetResolution(10)  # 设置等边多边形的边数

    mapper = vtk.vtkPolyDataMapper()  # vtkPolyDataMapper继承自vtkMapper 将输入的数据转换为几何图元（点、线、多边形）
    mapper.SetInputConnection(source.GetOutputPort())

    actor = vtk.vtkActor()    # vtkActor 继承 vtkProp类
    # 演员添加映射器
    actor.SetMapper(mapper)
    # 绘制器
    ren = vtk.vtkRenderer()
    # 绘制器添加演员
    ren.AddActor(actor)
    ren.SetBackground(0.1, 0.5, 0.4)  # 设置渲染场景的背景颜色， 用R G B的格式设置，三个分量的取值为0.0~1.0
    ren.SetBackground2(0.5, 0.1, 0.1)  # SetBackground2() 用于设置渐变的另外一种颜色
    ren.SetGradientBackground(1)  # 设置渐变开关为开 0为关闭

    myLight = vtk.vtkLight()
    myLight.SetColor(0, 1, 0)
    myLight.SetPosition(0, 0, 1)
    myLight.SetFocalPoint(ren.GetActiveCamera().GetFocalPoint())
    ren.AddLight(myLight)

    myLight2 = vtk.vtkLight()
    myLight2.SetColor(0, 0, 1)
    myLight2.SetPosition(0, 0, -1)
    myLight2.SetFocalPoint(ren.GetActiveCamera().GetFocalPoint())
    ren.AddLight(myLight2)

    # 绘制窗口
    renWin = vtk.vtkRenderWindow()
    # 绘制窗口添加绘制器
    renWin.AddRenderer(ren)
    renWin.SetSize(400, 400)

    # 创建窗口交互器
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)
    iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())  # 设置交互器样式

    iren.Initialize()
    # 窗口读取绘制器生成的图形
    renWin.Render()
    iren.Start()


if __name__ == '__main__':
    cylinder_show()
